using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class DynamicHeaderListView : ListView
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    
    private const int LVM_SETHEADERHEIGHT = 0x1000 + 31;
    private const int LVM_GETHEADERHEIGHT = 0x1000 + 32;

    // 当前标题高度
    public int HeaderHeight
    {
        get
        {
            // 获取当前高度
            return (int)SendMessage(Handle, LVM_GETHEADERHEIGHT, IntPtr.Zero, IntPtr.Zero);
        }
        set
        {
            if (value > 0 && value != HeaderHeight)
            {
                // 设置新高度
                SendMessage(Handle, LVM_SETHEADERHEIGHT, (IntPtr)value, IntPtr.Zero);
                // 强制重绘
                Invalidate();
            }
        }
    }

    public DynamicHeaderListView()
    {
        View = View.Details;
        OwnerDraw = true;
        DrawColumnHeader += OnDrawColumnHeader;
        DrawItem += (s, e) => e.DrawDefault = true;
        DrawSubItem += (s, e) => e.DrawDefault = true;
    }

    private void OnDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        // 绘制背景（使用当前实际高度）
        using (var brush = new SolidBrush(Color.LightGray))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }

        // 绘制文本（垂直居中）
        var format = new StringFormat
        {
            Alignment = StringAlignment.Near,
            LineAlignment = StringAlignment.Center
        };

        e.Graphics.DrawString(e.Header.Text, Font, Brushes.Black, e.Bounds, format);

        // 绘制边框
        e.Graphics.DrawRectangle(Pens.DarkGray, e.Bounds);
    }

    // 提供外部调用的动态修改高度方法
    public void UpdateHeaderHeight(int newHeight)
    {
        // 确保控件已创建句柄
        if (IsHandleCreated)
        {
            HeaderHeight = newHeight;
        }
        else
        {
            // 句柄未创建时，延迟到句柄创建后设置
            HandleCreated += (s, e) => HeaderHeight = newHeight;
        }
    }
}

// 使用示例
public class TestForm : Form
{
    private DynamicHeaderListView listView;
    private TrackBar heightAdjuster;

    public TestForm()
    {
        Text = "动态调整标题高度示例";
        Size = new Size(600, 400);

        // 创建高度调整滑块
        heightAdjuster = new TrackBar
        {
            Minimum = 20,
            Maximum = 100,
            Value = 30,
            Dock = DockStyle.Top,
            TickFrequency = 10
        };
        heightAdjuster.ValueChanged += HeightAdjuster_ValueChanged;

        // 创建列表视图
        listView = new DynamicHeaderListView
        {
            Dock = DockStyle.Fill
        };

        // 添加列
        listView.Columns.Add("ID", 80);
        listView.Columns.Add("名称", 150);
        listView.Columns.Add("说明", 300);

        // 添加测试数据
        listView.Items.Add(new ListViewItem(new[] { "1", "项目A", "可动态调整标题高度" }));

        // 布局控件
        Controls.Add(listView);
        Controls.Add(heightAdjuster);

        // 初始高度
        listView.UpdateHeaderHeight(30);
    }

    // 滑块变动时动态修改高度
    private void HeightAdjuster_ValueChanged(object sender, EventArgs e)
    {
        listView.UpdateHeaderHeight(heightAdjuster.Value);
    }
}
